package com.example.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sms.entity.Student;
import com.example.sms.entity.dto.LoginDTO;
import com.example.sms.entity.dto.RegisterDTO;
import com.example.sms.exception.R;
import com.example.sms.mapper.StudentMapper;
import com.example.sms.response.ResponseModel;
import com.example.sms.service.StudentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/student")
@Tag(name = "学生管理", description = "学生管理相关接口")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentMapper studentMapper;
    @Operation(summary = "获取学生信息")
    @GetMapping("info")
    public Object info(Integer id){
        return R.ok(studentService.getById(id));
    }
    @Operation(summary = "获取所有课程信息")
    @GetMapping("list")
    public Object list(Integer id){
        //检查是否有该学生
        if (studentService.getById(id) == null)
            return R.fail("该学生不存在");
        log.info("获取学生课程信息：" + id);
        return R.ok(studentService.listSelfCourse(id));
    }
    @Operation(summary = "修改学生信息")
    @PostMapping("updateStudentInfo")
    public Object update(@RequestBody Student student){
        //检查是否有该学生
        Student student1 = studentService.getOne(new QueryWrapper<Student>().lambda()
                .eq(Student::getUsername, student.getUsername())
                .eq(Student::getPassword, student.getPassword()));
        if (student1 == null)
            return R.fail("该学生不存在");
        else {
        log.info("修改学生信息 ：" + student.getName());
        student.setId(student1.getId());
        return R.ok(studentService.updateById(student));
        }
    }
}
